package com.hfjyz.registration.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.hfjyz.registration.command.OrderAnalyzeCommand;
import com.hfjyz.registration.contants.ExamContants;
import com.hfjyz.registration.model.Exam;
import com.hfjyz.registration.object.OrderAnalyzeInfoBean;
import com.hfjyz.registration.service.OrderInfoService;

public class OrderAnalyzeController extends SimpleFormController {
	private OrderInfoService orderInfoService;

	@SuppressWarnings("rawtypes")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		OrderAnalyzeCommand cmd = (OrderAnalyzeCommand) command;

		Exam exam = (Exam) request.getSession().getAttribute(
				ExamContants.SESSION_KEY_EXAM_INFO);
		cmd.setExamId(exam.getId());
		if (cmd.getAnalyzeType() == null) {
			cmd.setAnalyzeType(1);
		}
		List<OrderAnalyzeInfoBean> printInfos = orderInfoService
				.getOrderAnalyzeInfo(cmd);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("orderAnalyzeInfos", printInfos);

		return model;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		OrderAnalyzeCommand cmd = (OrderAnalyzeCommand) command;
		Exam exam = (Exam) request.getSession().getAttribute(
				ExamContants.SESSION_KEY_EXAM_INFO);
		cmd.setExamId(exam.getId());

		List<OrderAnalyzeInfoBean> printInfos = orderInfoService
				.getOrderAnalyzeInfo(cmd);

		Map model = errors.getModel();
		model.put("orderAnalyzeInfos", printInfos);

		return new ModelAndView(getSuccessView(), model);
	}

	/**
	 * @return the orderInfoService
	 */
	public OrderInfoService getOrderInfoService() {
		return orderInfoService;
	}

	/**
	 * @param orderInfoService
	 *            the orderInfoService to set
	 */
	public void setOrderInfoService(OrderInfoService orderInfoService) {
		this.orderInfoService = orderInfoService;
	}
}
